<?php
App::uses('AppController', 'Controller');
/**
 * Guests Controller
 * Author : CuongNV
 */
class GuestsController extends AppController {

	public function admin_index() {
		$this->Guest->recursive = 0;
		$this->set('guests', $this->paginate());
	}
	
	public function admin_view($id = null) {
		if (!$this->Guest->exists($id)) {
			throw new NotFoundException(__d('croogo', 'Invalid guest'));
		}
		$options = array('conditions' => array('Guest.' . $this->Guest->primaryKey => $id));
		$this->set('guest', $this->Guest->find('first', $options));
	}

	public function list_guest(){
		$this->Guest->recursive = 1;
		$this->Paginator->settings = array('order' => array('Guest.name' => 'ASC'));
		return $this->Paginator->paginate('Guest');
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->Guest->create();
			if ($this->Guest->save($this->request->data)) {
				$this->redirect(array('controller'=>'rooms','action' => 'index'));
			} else {
				$this->redirect(array('controller'=>'rooms','action' => 'index'));
			}
		}
	}


	public function add_cus(){
		$this->autoRender = false;
		if ($this->request->is('post')) {
			$this->Guest->create();
			$this->Guest->save($this->request->data);
		}
	}
	
	public function infomation(){
		$this->autoRender = false;
		$id = $_GET['id'];
		$this->Guest->recursive = 2;
		$data = $this->Guest->find('all', array('conditions' => array('Guest.id' => $id)));
		$date_current = date('Y-m-d H:i:s');
		foreach ($data[0]['Reservation'] as $key => $value) {
			if($value['status'] == 0){
				unset($data[0]['Reservation'][$key]);
			}
		}
		echo json_encode($data);
	}

	public function del(){
		$this->autoRender = false;
		$data = json_decode($_GET["data"]);
		$this->Guest->delete($data->id);
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Guest->exists($id)) {
			$this->redirect(array('controller' => 'pages', 'action' => 'home'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['Guest']['id'] = $id;
			$this->Guest->save($this->request->data);
			$this->redirect(array('controller' => 'pages', 'action' => 'home'));
		} else {
			$this->Guest->recursive = -1;
			$options = array('conditions' => array('Guest.' . $this->Guest->primaryKey => $id));
			$this->request->data = $this->Guest->find('first', $options);
			$this->loadModel('Country');
			$country = $this->Country->find('list');
			$this->set(compact('country'));
		}
	}

	public function del_guest_waiting(){
		$this->autoRender = false;
		$data = json_decode($_GET["data"]);
		$id = $data->id;
		$temp = explode(",", $id);
		$this->Guest->deleteAll(array('Guest.id'=>$temp),false);
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Guest->id = $id;
		if (!$this->Guest->exists()) {
			throw new NotFoundException(__d('croogo', 'Invalid guest'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Guest->delete()) {
			$this->Session->setFlash(__d('croogo', 'Guest deleted'), 'default', array('class' => 'success'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__d('croogo', 'Guest was not deleted'), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}

	public function delete($id = null) {
		$this->Guest->id = $id;
		if (!$this->Guest->exists()) {
			return $this->redirect(array('controller'=>'pages','action' => 'home'));
		}
		$this->Guest->delete();
		return $this->redirect(array('controller'=>'pages','action' => 'home'));
	}

}
